#include "../include/RedisServer.hpp"

#include <vector>

using std::cout; 
using std::endl;
using std::string; 
using std::vector;

//建立连接
bool MyRedis::connect(string host, int port) 
{ 
    _pConnect = redisConnect(host.c_str(), port);
    if(_pConnect != nullptr && _pConnect->err)
    { 
        cout << "connect error : " << _pConnect->errstr << endl; 
        return false;
    }
    return true;
}

//获取value值
string MyRedis::getValues(string key1,string key2)
{
    /* cout << "get value key1"<<key1 << "key2 :"<<key2 << endl; */
    _pReply = (redisReply*)redisCommand(_pConnect, "HGET  %s %s", key1.c_str(),key2.c_str());//得到的时候摘要 
    string str;
    /* cout << "getValues :" << _pReply->type << endl; */ 
    if(_pReply->type == REDIS_REPLY_STRING)//返回的是字符串
    {
        str = _pReply->str;
        cout  << "values succes" << endl;
    }
    else if(_pReply->type == REDIS_REPLY_NIL)
    {
        cout << "NIL" << endl;
    }
    else
    {
        cout << "some error " << endl;
    }
    freeReplyObject(_pReply); 
    _pReply = nullptr;
    return str;
}

//获取key值
bool MyRedis::getKeys(string key,std::vector<string> & cache)
{
    _pReply = (redisReply*)redisCommand(_pConnect, "HKEYS  %s ", key.c_str());//得到的时候摘要 

    /* cout <<"get ：" <<_pReply->type << endl; */

    if(_pReply->type == REDIS_REPLY_NIL)//失败情况 没有数据返回
    { 
        printf("get nil\n");
    }
    else if(_pReply->type==REDIS_REPLY_ERROR)//表示出错，查看出错信息
    {
        cout << "ERROR  " << endl;
        freeReplyObject(_pReply); 
        _pReply = nullptr;
        return false;
    }
    else if(_pReply->type == REDIS_REPLY_ARRAY)
    {
        /* cout << "size :" << _pReply->elements << endl; */
        if(_pReply->elements == 0)
        {
            return false;
        }
        for(size_t i =0;i<_pReply->elements;++i)
        {
            /* cout << _pReply->element[i]->str <<endl; */
            cache.push_back(_pReply->element[i]->str);
        }
        freeReplyObject(_pReply); 
        _pReply = nullptr;
        return true;
    }
}

//设置key key value
void MyRedis::set(string key1,string key2 ,string value) 
{ 
    /* cout << "key" << key1 << "key2 " << key2<< endl; */

    _pReply = (redisReply*)redisCommand(_pConnect, "HSET %s  %s  %s",
                                        key1.c_str(),key2.c_str(), value.c_str()); 


    /* cout << "set " << _pReply->type << endl; */

    if(_pReply == NULL)
    {
        printf("command error\n");
        redisFree(_pConnect);
        return ;
    }
    else if(_pReply->type == REDIS_REPLY_NIL)
    {
        printf("command error\n");
    }
    cout << "set succes" <<endl;
    freeReplyObject(_pReply);
    _pReply = nullptr;
} 

void MyRedis::setOutTime(string key1)
{
    _pReply = (redisReply*)redisCommand(_pConnect, "expire  %s  %d",
                                        key1.c_str(), 60 );
    _pReply = nullptr;
}

/* bool MyRedis::findIt(string & sought) */
/* { */
/*     string URL =getKeys(sought); */
/*     string Summay =getValues(sought); */
/* } */
